"""
@Author  : 康帅
@Time    : 2021/3/26 16:13
@Function: 处理时间的脚本
"""
import time
import datetime
from datetime import datetime


def get_now():
    """ 获取当前时间"""
    # 方法一
    now = datetime.datetime.now()  # 2021-03-26 16:15:48.662004
    print(now)
    # 方法二
    times = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())  # 2021-03-26 16:40:16
    print(times)


def get_now_add_half_an_hour():
    """ 当前时间增加半小时 """
    now = datetime.datetime.now()
    now_add = now + datetime.timedelta(hours=0.5)  # 2021-03-26 16:51:09.602344
    now_add = now_add.strftime('%Y-%m-%d %H:%M:%S')  # 2021-03-26 16:51:52
    print(now_add)


def get_tomorrow():
    """ 获取明天/前N天 """
    date = datetime.date.today() + datetime.timedelta(days=1)
    print(date)


def get_now_day_start_and_end():
    """ 获取当天开始和结束时间"""
    date = datetime.datetime.combine(datetime.date.today(), datetime.time.min)  # 2021-03-26 00:00:00
    print(date)
    date = datetime.datetime.combine(datetime.date.today(), datetime.time.max)  # 2021-03-26 23:59:59.999999
    print(date)


def get_date_years_month_day():
    """ 获取现在的年月日 """
    years_month_day = datetime.datetime.now().date()  # 2021-03-26
    print(years_month_day)


def get_current_date():
    """ 获取现在的日期 """
    current = datetime.date.today()  # 2021-03-26 17:03:18
    print(current)


def time_tuple():
    local_time = time.localtime()  # time.struct_time(tm_year=2021, tm_mon=3, tm_mday=26, tm_hour=16, tm_min=33, tm_sec=18, tm_wday=4, tm_yday=85, tm_isdst=0)
    print(local_time)


def time_stamp():
    times = time.time()  # 1616747598.4224167
    print(times)


def second_conversion_minutes_and_seconds(seconds):
    """
    秒转时分
    :param seconds:
    :return: 0h0m0s
    """
    m, s = divmod(seconds, 60)
    h, m = divmod(m, 60)
    return "%01dh%01dm%01ds" % (h, m, s)


def minutes_and_seconds():
    """
    时分秒分开显示
    :return:
    """
    t = datetime.time(1, 2, 3)
    print(t)
    print('hour       :', t.hour)
    print('minute     :', t.minute)
    print('second     :', t.second)
    print('microsecond:', t.microsecond)
    print('tzinfo     :', t.tzinfo)


def datetime_time_minmax():
    """
    获取天 最大和最小时间
    :return:
    """
    print('min_datetime:', datetime.time.min)
    print('max_datetime:', datetime.time.max)


def datetime_date_minmax():
    """
    获取年 最大日期和最小日期
    :return:
    """
    print(f'min_date:{datetime.date.min}')
    print(f'max_date:{datetime.date.max}')


def date_datetime_min():
    """
    获取当天最小时间
    适用于查询筛选当天数据
    :return:
    """
    date = str(datetime.date.today())
    min_time = str(datetime.time.min)
    date_min_time = date + " " + min_time
    print(date_min_time)


def date_datetime_max():
    """
    获取当天最大时间
    适用于查询筛选当天数据
    :return:
    """
    date = str(datetime.date.today())
    max_time = str(datetime.time.max)
    date_max_time = date + " " + max_time
    print(date_max_time)


def date_calculation():
    """
    日期运算
    :return:
    """
    today = datetime.date.today()
    print('Today:', today)

    one_day = datetime.timedelta(days=1)
    print('One day  :', one_day)

    yesterday = today - one_day
    print('Yesterday:', yesterday)

    tomorrow = today + one_day
    print('Tomorrow :', tomorrow)


def datetime_timedelta_math():
    """
    日期运算浮点数
    :return:
    """
    one_day = datetime.timedelta(days=1)
    print('1 day    :', one_day)
    print('5 days   :', one_day * 5)
    print('1.5 days :', one_day * 1.5)
    print('1/4 day  :', one_day / 4)

    # 假设午饭时间为 1 小时
    work_day = datetime.timedelta(hours=7)
    meeting_length = datetime.timedelta(hours=1)
    print('meetings per day :', work_day / meeting_length)


def datetime_datetime_combine():
    """
    日期与时间组合
    :return:
    """
    t = datetime.time(1, 2, 3)
    d = datetime.date.today()
    dt = datetime.datetime.combine(d, t)
    print(dt)


def datetime_strptime():
    """
    字符串 和 时间格式数据转换
    :return:
    """
    text = '2021-07-28'
    y = datetime.strptime(text, '%Y-%m-%d')
    z = datetime.now()
    diff = z - y
    print(diff)


def parse_ymd(s):
    """
    该时间处理性能要比strptime好，要比strptime快7倍
    前提是已知要处理时间的数据格式
    :param s:
    :return:
    """
    year_s, mon_s, day_s = s.split('-')
    print(datetime(int(year_s), int(mon_s), int(day_s)))


if __name__ == '__main__':
    get_now()
    get_now_add_half_an_hour()
    get_date_years_month_day()
    get_current_date()
    time_tuple()
    time_stamp()
    get_tomorrow()
    get_now_day_start_and_end()
    second_conversion_minutes_and_seconds(100)
    minutes_and_seconds()
    datetime_time_minmax()
    datetime_date_minmax()
    date_calculation()
    datetime_timedelta_math()
    datetime_datetime_combine()
    datetime_strptime()
    parse_ymd(s="2021-07-28")
    date_datetime_min()
    date_datetime_max()
